/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Vistor;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.VistorRepository;
import me.zhengjie.modules.system.service.VistorService;
import me.zhengjie.modules.system.service.dto.VistorDto;
import me.zhengjie.modules.system.service.dto.VistorQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.VistorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class VistorServiceImpl implements VistorService {

    private final VistorRepository vistorRepository;
    private final VistorMapper vistorMapper;

    @Override
    public Map<String,Object> queryAll(VistorQueryCriteria criteria, Pageable pageable){
        Page<Vistor> page = vistorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(vistorMapper::toDto));
    }

    @Override
    public List<VistorDto> queryAll(VistorQueryCriteria criteria){
        return vistorMapper.toDto(vistorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public VistorDto findById(Long id) {
        Vistor vistor = vistorRepository.findById(id).orElseGet(Vistor::new);
        ValidationUtil.isNull(vistor.getId(),"Vistor","id",id);
        return vistorMapper.toDto(vistor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VistorDto create(Vistor resources) {
        resources.setCid(0l);
        return vistorMapper.toDto(vistorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Vistor resources) {
        Vistor vistor = vistorRepository.findById(resources.getId()).orElseGet(Vistor::new);
        ValidationUtil.isNull( vistor.getId(),"Vistor","id",resources.getId());
        vistor.copy(resources);
        vistorRepository.save(vistor);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            vistorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<VistorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VistorDto vistor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", vistor.getCreateTime());
            map.put("更新时间", vistor.getUpdateTime());
            map.put("创建者", vistor.getCreateBy());
            map.put("更新者", vistor.getUpdateBy());
            map.put("所属访问管理", vistor.getVisitId());
            map.put("访客姓名", vistor.getVisitorName());
            map.put("访客主键", vistor.getVisitorId());
            map.put("访客手机号", vistor.getVistorPhone());
            map.put("访客身份证", vistor.getVisitorIdCard());
            map.put("临时标签卡", vistor.getTempLabelCard());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}